package com.auto.qinglong.net.client

import androidx.paging.Pager
import androidx.paging.PagingConfig
import com.auto.base.net.ResultWrapper
import com.auto.base.util.GsonUtil
import com.auto.qinglong.net.client.beans.LoginBean
import com.auto.qinglong.net.client.beans.QlAccount
import com.auto.qinglong.net.client.beans.QueryTaskBean
import com.auto.qinglong.net.client.beans.Task
import com.auto.qinglong.net.client.beans.TaskGroupTagBean
import com.auto.qinglong.net.client.beans.TaskResultBean
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import javax.inject.Inject


class QlRepository @Inject constructor(val apiQl: ApiQl) {

    suspend fun login(user: QlAccount): ResultWrapper<LoginBean> {
        return try {
            val result = apiQl.login(user)
            if (result.code == 200 && result.data != null) {
                ResultWrapper.Success(result.data)
            } else {
                ResultWrapper.Error(Exception(result.message))
            }
        } catch (e: Exception) {
            ResultWrapper.Error(e)
        }
    }

    fun getTaskPager(
        search: String, bean: TaskGroupTagBean? = null
    ): Pager<Int, Task> {
        return Pager(
            config = PagingConfig(
                pageSize = 10, enablePlaceholders = false
            ), pagingSourceFactory = {
                TaskPagingSource(
                    repository = this, search = search, bean = bean
                )
            })
    }


    suspend fun getTask(
        searchValue: String, page: Int = 1, size: Int = 10, bean: TaskGroupTagBean? = null
    ): ResultWrapper<TaskResultBean> {
        return try {
            val query = if (bean != null && !bean.filters.isNullOrEmpty()) {
                GsonUtil.toJson(QueryTaskBean(bean.filters, bean.filterRelation))
            } else {
                ""
            }

            val result = apiQl.getTasks(searchValue, page, size, queryString = query)
            if (result.code == 200 && result.data != null) {
                ResultWrapper.Success(result.data)
            } else {
                ResultWrapper.Error(Exception(result.message))
            }
        } catch (e: Exception) {
            ResultWrapper.Error(e)
        }
    }

    suspend fun runTask(list: List<String>): ResultWrapper<Any> {
        return try {
            val result = apiQl.runTasks(list)
            if (result.code == 200) {
                ResultWrapper.Success(null)
            } else {
                ResultWrapper.Error(Exception(result.message))
            }
        } catch (e: Exception) {
            ResultWrapper.Error(e)
        }
    }

    suspend fun getViews(): ResultWrapper<List<TaskGroupTagBean>> {
        return try {
            val result = apiQl.getViews()
            if (result.code == 200 && !result.data.isNullOrEmpty()) {
                ResultWrapper.Success(result.data)
            } else {
                ResultWrapper.Error(Exception(result.message))
            }
        } catch (e: Exception) {
            ResultWrapper.Error(e)
        }
    }

    suspend fun getTaskLog(taskId: String): ResultWrapper<String> {
        return try {
            val result = apiQl.getTaskLog(taskId = taskId)
            if (result.code == 200) {
                ResultWrapper.Success(result.data)
            } else {
                ResultWrapper.Error(Exception(result.message))
            }
        } catch (e: Exception) {
            ResultWrapper.Error(e)
        }
    }

    suspend fun deleteTask(ids: Task): ResultWrapper<String> {
        return deleteTask(listOf(ids.key))
    }

    suspend fun deleteTask(ids: List<String>): ResultWrapper<String> {
        return try {
            val body =
                RequestBody.create("application/json".toMediaTypeOrNull(), GsonUtil.toJson(ids))
            val result = apiQl.deleteTasks(body)
            if (result.code == 200) {
                ResultWrapper.Success(result.data)
            } else {
                ResultWrapper.Error(Exception(result.message))
            }
        } catch (e: Exception) {
            ResultWrapper.Error(e)
        }
    }
}